package org.tangdao.modules.file.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tangdao.common.lang.StringUtils;
import org.tangdao.common.suports.DataEntity;
import org.tangdao.modules.file.mapper.FileEntityMapper;
import org.tangdao.modules.file.model.domain.FileEntity;
import org.tangdao.modules.file.service.IFileEntityService;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;

/**
 * @ClassName: FileEntity
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author RuYang
 * @date 2019/03/07
 * 
 */

@Service
public class FileEntityServiceImpl implements IFileEntityService {

	@Autowired
	FileEntityMapper fileEntityMapper;

	@Autowired
	private FileUploadServiceExtend fileUploadServiceExtend;

	public FileEntity getByMd5(String md5) {
		FileEntity fileEntity = null;
		if (StringUtils.isNotBlank(md5)) {
			fileEntity = fileEntityMapper.selectOne(Wrappers.<FileEntity>lambdaQuery().eq(FileEntity::getFileMd5, md5));
		}
		if (fileEntity == null) {
			fileEntity = new FileEntity();
			fileEntity.setFileMd5(md5);
		}
		fileEntity.setStatus(DataEntity.STATUS_DELETE);
		if (StringUtils.isNotBlank(fileEntity.getFileId()) && this.fileUploadServiceExtend.fileExists(fileEntity)) {
			fileEntity.setStatus(DataEntity.STATUS_NORMAL);
		}
		return fileEntity;
	}

	public boolean insert(FileEntity fileEntity) {
		return SqlHelper.retBool(fileEntityMapper.insert(fileEntity));
	}
}
